package com.example.menuvirtualclient.rule;

import java.util.List;

import android.content.Context;

import com.example.menuvirtualclient.core.*;
import com.example.menuvirtualclient.data.ISession;
import com.example.menuvirtualclient.data.SessionFactory;
import com.example.menuvirtualclient.repository.*;

public class DishRule {

	private ISession _session;
	private final DishRepository _repository;

	public DishRule(Context context) {
		_session = SessionFactory.GetSession(context);
		_repository = new DishRepository(context);
	}
	
		
	public List<Dish> GetDishesByMenu(int menuId) throws Exception {
		try {
			_session.Open();
			return _repository.GetDishesByMenu(menuId);
		} 
		catch (Exception ex) {
			throw ex;
		}
		finally{
			_session.Close();
		}
	}
	
	
	public List<Dish> GetList() throws Exception {
		try {
			_session.Open();
			return _repository.GetAll();
		} 
		catch (Exception ex) {
			throw ex;
		}
		finally{
			_session.Close();
		}
	}
	
	public Dish Get(int id) throws Exception{
		try {
			_session.Open();
			return _repository.Get(id);			
		} 
		catch (Exception ex) {
			throw ex;
		}
		finally{
			_session.Close();
		}
	}
	
	public void Insert(Dish entity) throws Exception{
		try {
			_session.Open();
			_repository.Insert(entity);			
		} 
		catch (Exception ex) {
			throw ex;
		}
		finally{
			_session.Close();
		}
	}
	
	public void Update(Dish entity) throws Exception{
		try {
			_session.Open();
			_repository.Update(entity);			
		} 
		catch (Exception ex) {
			throw ex;
		}
		finally{
			_session.Close();
		}
	}
	
	public List<Comment> GetCommnetsByDish(int dishid) throws Exception {
		try {
			_session.Open();
			return _repository.GetCommnetsByDish(dishid);
		} 
		catch (Exception ex) {
			throw ex;
		}
		finally{
			_session.Close();
		}
	}
	
	public void saveDishComment(List<DishComment> dishComments) throws Exception{
		try {
			_session.Open();
			for(DishComment entity : dishComments){
				if(!_repository.exist(entity)){
					_repository.saveDishComment(entity);
				}
			}
		} 
		catch (Exception ex) {
			throw ex;
		}
		finally{
			_session.Close();
		}
	}
	
	public boolean exist(DishComment entity) throws Exception {
		try {
			_session.Open();
			return _repository.exist(entity);
		} 
		catch (Exception ex) {
			throw ex;
		}
		finally{
			_session.Close();
		}
	}
	
}
